#ifndef VECTOR2_H
#define VECTOR2_H

#include "typedefs.h"
#include <Eigen/Core>
#include <QString>

namespace Math
{
    class Vector2
    {
        #ifdef USE_FLOAT
            Eigen::Vector2f v;
        #else
            Eigen::Vector2d v;
        #endif

        public:
        #ifdef USE_FLOAT
            Eigen::Vector2f getEigen() const {return v;}
        #else
            Eigen::Vector2d getEigen() const {return v;}
        #endif

            Vector2();

            Vector2(const Vector2 &a) { v = a.v;}

#ifdef USE_FLOAT
            Vector2(const Eigen::Vector2f &ev) { v = ev; }
#else
            Vector2(const Eigen::Vector2d &ev) { v = ev; }
#endif
            Vector2(const Real &x, const Real &y) { v[0] = x;v[1] = y; }


            Vector2(bool zero) { if(zero) setValue(0);  }

            void set(const Vector2 &val) { v = val.v; }

            void set(const Real &x, const Real &y) { v[0] = x;v[1] = y;}

            void setValue(const Real &val) { v[0] = val; v[1] = val;  }

            Real x(void) const { return v[0]; }
            Real y(void) const { return v[1]; }

            void x(const Real &a) { v[0] = a; }
            void y(const Real &a) { v[1] = a; }

            //Vector2& operator=(const Vector2& b) { v = b.v; return *this;}

            Vector2 operator*(const Real &a) { return Vector2(v*a); }

            void operator*=(const Real &a) { v *= a; }

            Vector2 operator-(const Vector2& b) const {
                return Vector2(v - b.getEigen());
            }

            void normalize() { v.normalize(); }

            Real getLength() const { return v.norm(); }

            Real getSquaredLength() const { return v.squaredNorm(); }

            QString toString() const {
                QString res;
                QString sx,sy;
                sx.setNum(x());
                sy.setNum(y());
                res = sx + " " + sy;
                return res;
            }

            static Vector2 fromString(const QString &s) {
                std::stringstream ss(s.toStdString());
                Real x,y;
                ss >> x >> y;
                return Vector2(x,y);
            }

            Real operator[](const int &i) const { return v[i];}

            static Vector2 ZERO;
    };
}



#endif // VECTOR2_H
